package com.practise.rabbitmqdemo.controller;

import com.alibaba.fastjson.JSON;
import com.practise.rabbitmqdemo.config.DirectRabbitConfig;
import com.practise.rabbitmqdemo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "direct")
@Api(value = "send DirectMsg controller", tags = {"direct消息队列测试接口"})
public class DirectMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "hello", httpMethod = "POST")
    @RequestMapping(value = "sayHi")
    public String sayHi(String userName, String msg) {
        return "hi,".concat(userName).concat(",").concat(msg);
    }

    @ApiOperation(value = "添加用户到Q1", httpMethod = "POST")
    @RequestMapping(value = "sendToQueue1")
    public void add(@RequestBody @ApiParam(name = "User", value = "测试生成类", required = true) User user) {
        log.info("add user to routing1");
        rabbitTemplate.convertAndSend(DirectRabbitConfig.EXCHANGE_1,DirectRabbitConfig.ROUTING_1, JSON.toJSONString(user));
        return;
    }

    @ApiOperation(value = "添加消息到Q2", httpMethod = "POST")
    @RequestMapping(value = "sendToQueue2")
    public String sendToQueue(String userName, String msg) {
        String concatMsg = "hi,".concat(userName).concat(",").concat(msg);
        rabbitTemplate.convertAndSend(DirectRabbitConfig.EXCHANGE_2, DirectRabbitConfig.ROUTING_2,concatMsg);
        return concatMsg;
    }

    @ApiOperation(value = "添加消息到Q11", httpMethod = "POST")
    @RequestMapping(value = "sendToQueue11")
    public String sendToQueue11(String msg) {
        String concatMsg = "hi,".concat(msg);
        rabbitTemplate.convertAndSend(DirectRabbitConfig.EXCHANGE_1, DirectRabbitConfig.ROUTING_11,concatMsg);
        return concatMsg;
    }


    /* 模拟消息丢失
    ①消息推送到server，但是在server里找不到交换机
    ②消息推送到server，找到交换机了，但是没找到队列
    ③消息推送到sever，交换机和队列啥都没找到
    ④消息推送成功*/

    @ApiOperation(value = "添加消息到不存在的交换机", httpMethod = "POST")
    @RequestMapping(value = "sendToNotExistExchange")
    public String sendToNotExistExchange(String msg) {
        log.info("添加消息到不存在的交换机 404 ");
        String concatMsg = "hi,".concat(msg);
        rabbitTemplate.convertAndSend("NotExistExchange", DirectRabbitConfig.ROUTING_11,concatMsg);
        return concatMsg;
    }

    @ApiOperation(value = "添加消息到不存在的队列", httpMethod = "POST")
    @RequestMapping(value = "sendToNotExistQueue")
    public String sendToNotExistQueue(String msg) {
        String concatMsg = "hi,".concat(msg);
        log.info("消息推送到server，找到交换机了，但是没找到队列");
        // 交换机没有绑定队列
        rabbitTemplate.convertAndSend(DirectRabbitConfig.LONELY_DIRECT_EXCHANGE, "rk",concatMsg);
        return concatMsg;
    }

    @ApiOperation(value = "添加消息 没交换机没队列", httpMethod = "POST")
    @RequestMapping(value = "sendToNotExistExchangeQueue")
    public String sendToNotExistExchangeQueue(String msg) {
        String concatMsg = "hi,".concat(msg);
        log.info("消息推送到server，没交换机没队列");
        rabbitTemplate.convertAndSend("one", "two",concatMsg);
        return concatMsg;
    }
}
